<?php

class BkLoginController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
		$this->_helper->layout->disableLayout();
    }
	
	function indexAction()
	{
		$params = $this->_request->getParams();
		$error = 1;
        $this->view->title = "用户登录";
        $this->view->url = $params['url'];
        if ($this->_request->isPost()) {
        	if($this->_request->getPost('email') && $this->_request->getPost('pwd'))
        	{
        		$mod_admin = new Db_Tables_AdminUsers();
				$select = $mod_admin->select();
				$select->where("admin_email = ?", $this->_request->getPost('email'));
				$select->where("admin_pwd = ?", sha1($this->_request->getPost('pwd')));
				$select->where("admin_status = ?", 1);
				$row = $mod_admin->fetchRow($select);
				
				if($row)
				{
					$_SESSION['identity'] = array(
						'id' => $row['admin_id'],
						'email' => $row['admin_email'],
						'name' => $row['admin_name']
					);
					
					$error = 0;
				}
				
				if(!$error)
				{
					if($this->_request->getPost('url'))
        			{
        				$this->_redirect($this->_request->getPost('url'));
        			}else{
						$this->_redirect("/bk");
        			}
				}
        	}else{
        		$this->view->msg = "邮箱或密码不能为空";
        	}
		}
	}
	
	function logoutAction()
    {
        //disable layout for Login page
        $this->_helper->layout->disableLayout();
        
        $params = $this->_request->getParams();
        
        session_destroy();
        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect('/bk-login?url='.$params['url']);
    }

}